var CART = function () {
    this.cartLlist = [];
    this.conf = {
        update:'/app/shoppingCarUpdate.htm',
        list:'/app/shoppingCar.htm'
    }
}
CART.prototype = {
    setList:function (cartList) {
        var s = this;
        s.cartLlist = _.defaultsDeep(cartList,s.cartLlist);
    },
    format:function (list) {
        return _.chain(list).map(function (item) {
            return _.extend(item,{isChecked:false});
        }).value();
    },
    getLocalData:function () {
        return this.cartLlist;
    },
    getList:function () {
        var s = this;
        var promise = $.Deferred();
        $.ajax({
            type:'post',
            dataType:'json',
            url:s.conf.list
        }).then(function (data) {
            if(data.code == '1'){
                s.setList(data.list);
            }
            promise.resolve(data);
        },function (err) {
            console.log(err);
        });
        return promise;
    },
    add:function (productId,count) {
        var s = this;
        if(!_.isEmpty(productId)){
            var promise = $.Deferred();
            $.ajax({
                type:'post',
                dataType:'json',
                url:s.conf.update,
                data:{
                    productId:productId,
                    count:count,
                    operType:'add'
                }
            }).then(function (data) {
                if(data.code == '1'){
                    s.getList().then(function (list) {
                        promise.resolve({code:1,list:s.getLocalData()});
                    });
                }else{
                    promise.resolve(data);
                }

            },function (err) {
                console.log(err);
            });
            return promise;
        }
    },
    update:function (productId,count) {
        var s = this;
        if(!_.isEmpty(productId)){
            var promise = $.Deferred();
            $.ajax({
                type:'post',
                dataType:'json',
                url:s.conf.update,
                data:{
                    productId:productId,
                    count:count,
                    operType:'update'
                }
            }).then(function (data) {
                if(data.code == '1'){
                    s.getList().then(function (list) {
                        promise.resolve(s.getLocalData());
                    });
                }else{
                    promise.resolve(data);
                }

            },function (err) {
                console.log(err);
            });
            return promise;
        }
    },
    count:function (save) {
        var s = this;
        return _.chain(_.filter(s.cartLlist,{isChecked:true})).map(function (item) {
            if(!_.isEmpty(save) && save == 'save'){
                return _.parseInt(item.quantity) * _.parseInt(item.price1);
            }else{
                return _.parseInt(item.quantity) * _.parseInt(item.price2);
            }
        }).thru(function (value) {
            return _.sum(value);
        }).value();
    },
    checked:function (id,checked) {
        var s = this;
        var index = _.findIndex(s.cartLlist,{productId:id});
        s.cartLlist[index].isChecked = checked;
    }
};

module.exports.shopCart = new CART();